package com.damacheng009.future.alarm.job;

import java.util.List;

import org.nutz.dao.Dao;
import org.nutz.dao.TableName;

import com.damacheng009.future.alarm.crawler.SinaFutureDayCrawler;
import com.damacheng009.future.alarm.domain.TKLine;
import com.damacheng009.future.alarm.toolkit.DaoTool;
import com.damacheng009.future.alarm.toolkit.GlobalConfig;
import com.damacheng009.future.alarm.vo.KLineVo;

public class FutureDayKLines2DBJob {
	private static final int MAX = 200;
	
	// 每天收盘后更新一次日线数据
	public void run() {
		Dao dao = DaoTool.createDao(null);
		
		for (String code : GlobalConfig.sinaCodeList) {
			List<KLineVo> lines = new SinaFutureDayCrawler().getKLines(code);
			TableName.set(code);
			
			int i = 0;
			if (lines.size() > MAX) {
				i = lines.size() - MAX;
			}
			
			for (; i < lines.size(); ++i) {
				KLineVo vo = lines.get(i);
				TKLine line = new TKLine();
				
				line.setDate(vo.getD());
				line.setOpen(Float.parseFloat(vo.getO()));
				line.setClose(Float.parseFloat(vo.getC()));
				line.setHigh(Float.parseFloat(vo.getH()));
				line.setLow(Float.parseFloat(vo.getL()));
				line.setVolume(Integer.parseInt(vo.getV()));
				line.setOpid(Integer.parseInt(vo.getP()));
				
				dao.insertOrUpdate(line);
			}
		}
		
	}
	
}
